package com.conf.web;

import com.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.io.File;

/**
 * 文件删除
 * 获取工程路径 :  https://www.iteblog.com/java_get_file_absolute_path/
 */
@Component
@Slf4j
public class FileDel {
    /**
     * File路径以 &&拼接的
     * @param file
     */
    public void del(String file){
       if(StringUtils.isEmpty(file)){
           return;
       }
       try {
           //当前工程的相对路径
           File directory = new File("");//参数为空
           String relativePath = directory.getCanonicalPath();
           String[] files = file.split("&&");
           for (int i = 0; i < files.length; i++) {
               //文件的绝对路径
               String absolutePath = relativePath+"\\src\\main\\webapp\\"+files[i];
                File delFile = new File(absolutePath);
                boolean flag = delFile.delete();
                if(flag){
                    log.info("删除文件:{}",absolutePath);
                }
                else{
                    log.info("文件未删除:{}",absolutePath);
                }
           }
       }catch (Exception e){
                e.printStackTrace();
       }
    }
    public void path() throws Exception{
        File f = new File(this.getClass().getResource("/").getPath());
        System.out.println(f);

        //当前工程的相对路径
        File directory = new File("");//参数为空
        String courseFile = directory.getCanonicalPath() ;
        System.out.println(courseFile);
        System.out.println(directory.getAbsolutePath());
        System.out.println(this.getClass().getClassLoader().getResource("") );
        System.out.println(System.getProperty("user.dir"));
        System.out.println( System.getProperty("java.class.path"));
    }


    public static void main(String[] args) throws Exception{
        FileDel fileDel = new FileDel();
        fileDel.path();
    }
}
